package com.springdemo.annoationtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Component
public class ResourceTest {
    @Value("注入字符串")
    private String str;

    @Value("#{systemProperties['os.name']}")//注入系统属性
    private String pstr;


    @Value("classpath:test.txt")
    private Resource text;

    public void  toStrings(){
    System.out.println("注入字符串："+str);
    System.out.println("系统属性："+pstr);
        try {
            InputStream inputStream = text.getInputStream();
            StringBuffer sb=new StringBuffer();
            byte[] b=new byte[1024];
            int l=0;
            while ((l=inputStream.read(b))!=-1){
            sb.append(new String(b, 0, l,"UTF-8"));
            }
            inputStream.close();
            System.out.println("text:"+sb);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
